---
title: "DDD Regression"
author: "Andrei Munteanu"
date: "29/09/2021"
output:
  # bookdown::html_document2
   html_document:
     fig_width: 8 
     fig_height: 8 
---
<style type="text/css">
.main-container {
  max-width: 1800px;
  margin-left: auto;
  margin-right: auto;
}
</style>

```{r options, include=FALSE, echo=FALSE, cache=FALSE}
knitr::opts_knit$set(root.dir=getwd())
knitr::opts_chunk$set(cache.lazy=FALSE,cache=TRUE,warning=FALSE,echo=TRUE,comment='',prompt=T)
options(modelsummary_format_numeric_latex='plain')
knitr::opts_template$set(
  regular=list(include=TRUE,echo=TRUE,cache=FALSE),
  regular_cache=list(include=TRUE,echo=TRUE,cache=TRUE),
  invisible=list(include=FALSE,echo=FALSE,cache=FALSE,warning=FALSE,results=FALSE,message=FALSE),
  muted=list(include=TRUE,echo=TRUE,cache=FALSE,warning=FALSE,results=FALSE,message=FALSE),
  latex=list(include=TRUE,echo=TRUE,cache=FALSE))

```
## {.tabset}

### 1 HS Towns
First, define the control and treatment groups. Find the years when high schools open and assign the before/after period to both groups.
```{r, opts.label='muted'}
#Find assign opening year
# trt_towns_1<-c('ALESD','TOPOLOVENI')
trt_towns_1<-c(145,290)
opening_yrs_1<-openings %>%
  filter(town_hs_bac %in% trt_towns_1) %>%
  group_by(town_hs_bac) %>%
  slice(which.min(as.numeric(an))) %>%
  ungroup() %>%
  select(an,town_hs_bac) %>%
  rename(after=an)
#Randomly assign them to the control group
data_1_control<- data_regression %>% 
  filter(town_hs_bac %in% towns_1$town_hs_bac ) %>%
  mutate(treatment=F) %>%
  group_by(town_hs_bac) %>%
  mutate(after=ifelse(length(opening_yrs_1$after)!=1,
                      sample(x=opening_yrs_1$after,replace=T,size=1),
                      opening_yrs_1$after)) %>%
  mutate(after=ifelse(an>=after,T,F)) %>%
  mutate(policy=after*treatment) %>%
  filter(school_change==F) %>%
  group_by(town_hs_bac,an) %>%
  mutate(n_students_town_year=sum(!is.na(liceu_repartizat))) %>%
  mutate(an=as.factor(an))
data_1_trt<- data_regression %>% 
  filter(town_hs_bac %in% trt_towns_1 ) %>%
  mutate(treatment=T) %>%
  left_join(opening_yrs_1,by=c("town_hs_bac"="town_hs_bac")) %>%
  mutate(after=ifelse(an>=after,T,F)) %>%
  mutate(policy=after*treatment) %>%
  filter(school_change==F) %>%
  group_by(town_hs_bac,an) %>%
  mutate(n_students_town_year=sum(!is.na(liceu_repartizat))) %>%
  mutate(an=as.factor(an))
data_1<-bind_rows(data_1_control,data_1_trt)
```
Run regression without controls:
```{r, opts.label='regular'}
model_1_trt_x_yr_town<-feols(grad_perc~quart*policy+entrance_perc*quart+n_students_town_year*quart|
                    specializare_bac2+town_hs_bac+an+quart^town_hs_bac+quart^an,
                  cluster=~town_hs_bac,
                  data=data_1)
summary(model_1_trt_x_yr_town)
gc()
```
Run regression with more controls:
```{r, opts.label='regular'}
model_1_trt_x_yr_town_controls<-
  feols(grad_perc~quart*policy+entrance_perc*quart+n_students_town_year*quart|                                      specializare_bac2+town_hs_bac+an+quart^town_hs_bac+quart^an
        +Unemployment_hs_bac^quart+Wages_hs_bac^quart+drop_hs_hs_bac^quart,
                                      cluster=~town_hs_bac,
                                      data=data_1)
summary(model_1_trt_x_yr_town_controls)
gc()
```
### 2 HS Towns
First, define the control and treatment groups. Find the years when high schools open and assign the before/after period to both groups.
```{r, opts.label='muted'}
#Find assign opening year
# trt_towns_2<-c('TARGU NEAMT','TARGU LAPUS')
trt_towns_2<-c(240,256)
opening_yrs_2<-openings %>%
  filter(town_hs_bac %in% trt_towns_2) %>%
  group_by(town_hs_bac) %>%
  slice(which.min(as.numeric(an))) %>%
  ungroup() %>%
  select(an,town_hs_bac) %>%
  rename(after=an)
#Randomly assign them to the control group
data_2_control<- data_regression %>% 
  filter(town_hs_bac %in% towns_2$town_hs_bac ) %>%
  mutate(treatment=F) %>%
  group_by(town_hs_bac) %>%
  mutate(after=ifelse(length(opening_yrs_2$after)!=1,
                      sample(x=opening_yrs_2$after,replace=T,size=1),
                      opening_yrs_2$after)) %>%
  mutate(after=ifelse(an>=after,T,F)) %>%
  mutate(policy=after*treatment) %>%
  filter(school_change==F) %>%
  group_by(town_hs_bac,an) %>%
  mutate(n_students_town_year=sum(!is.na(liceu_repartizat))) %>%
  mutate(an=as.factor(an))
data_2_trt<- data_regression %>% 
  filter(town_hs_bac %in% trt_towns_2 ) %>%
  mutate(treatment=T) %>%
  left_join(opening_yrs_2,by=c("town_hs_bac"="town_hs_bac")) %>%
  mutate(after=ifelse(an>=after,T,F)) %>%
  mutate(policy=after*treatment) %>%
  filter(school_change==F) %>%
  group_by(town_hs_bac,an) %>%
  mutate(n_students_town_year=sum(!is.na(liceu_repartizat))) %>%
  mutate(an=as.factor(an))
data_2<-bind_rows(data_2_control,data_2_trt)
```
Run regression without controls:
```{r, opts.label='regular'}
model_2_trt_x_yr_town<-feols(grad_perc~quart*policy+entrance_perc*quart+n_students_town_year*quart|
                               specializare_bac2+town_hs_bac+an+quart^town_hs_bac+quart^an,
                             cluster=~town_hs_bac,
                             data=data_2)
summary(model_2_trt_x_yr_town)
gc()
```
Run regression with more controls:
```{r, opts.label='regular'}
model_2_trt_x_yr_town_controls<-feols(grad_perc~quart*policy+entrance_perc*quart+n_students_town_year*quart|
                                        specializare_bac2+town_hs_bac+an+quart^town_hs_bac+quart^an+Unemployment_hs_bac^quart+Wages_hs_bac^quart+drop_hs_hs_bac^quart,
                                      cluster=~town_hs_bac,
                                      data=data_2)
summary(model_2_trt_x_yr_town_controls)
gc()
```

### Pooled DDD
```{r, opts.label='regular'}
data_1$type<-"1"
data_2$type<-"2"
data<-bind_rows(data_1,data_2)

model_ddd_pooled<-feols(I(grad_perc*100)~quart_town*after*treatment+I(entrance_perc*100)*type+n_students_town_year*quart_town+type*quart_town+treatment*type+after*type+quart_town*type|
specializare_bac2^quart_town+town_hs_bac^quart_town+an^quart_town+Unemployment_hs_bac^quart_town+Wages_hs_bac^quart_town+drop_hs_hs_bac^quart_town,
                                      cluster=~town_hs_bac,
                                      data=data)
summary(model_ddd_pooled)
gc()
```


### Pooled DD
```{r, opts.label='regular'}
data_1$type<-"1"
data_2$type<-"2"
data<-bind_rows(data_1,data_2)

model_dd_pooled<-feols(I(grad_perc*100)~after*treatment+I(entrance_perc*100)+n_students_town_year+type+I(entrance_perc*100)*type+n_students_town_year*type|
specializare_bac2+town_hs_bac+an+Unemployment_hs_bac+Wages_hs_bac+drop_hs_hs_bac,
                                      cluster=~town_hs_bac,
                                      data=data)
summary(model_dd_pooled)
gc()
```

### Pooled admission
```{r, opts.label='regular'}
data_1$type<-"1"
data_2$type<-"2"
data<-bind_rows(data_1,data_2)



model_sorting_pooled<-feols(I(school_mean_yr*100)~quart_town*after*treatment+I(entrance_perc*100)*type+n_students_town_year*type+type|
specializare_bac2+town_hs_bac+an+Unemployment_hs_bac+Wages_hs_bac+drop_hs_hs_bac,
                                      cluster=~town_hs_bac,
                                      data=data)
summary(model_sorting_pooled)



gc()
```


### Latex
Latex tables:

```{r, opts.label='latex'}
print(modelsummary(list(model_sorting_pooled,model_dd_pooled,model_ddd_pooled), statistic=NULL,output="latex",
                   estimate="{estimate}{stars}",
                   gof_map = c("r.squared"),
                   stars=c('^{*}'=0.1,'^{**}'=0.05,'^{***}'=0.01)))
```
